package net.wanho.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.crypto.SecureUtil;
import lombok.SneakyThrows;
import net.wanho.contant.CommonConstant;
import net.wanho.contant.UserConstant;
import net.wanho.po.Member;
import net.wanho.mapper.MemberMapper;
import net.wanho.service.MemberService;
import net.wanho.vo.MemberVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.InputStream;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class MemberServiceImpl implements MemberService {

    private final MemberMapper memberMapper;

    /**
     * 会员表分页查询
     * @param pageNum
     * @param pageSize
     * @param member
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Map<String, Object> findPage(int pageNum, int pageSize, Member member) {
        if(ObjectUtil.isNull(member)){
            member = new Member();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<MemberVo> memberList = memberMapper.select(member);
        PageInfo<MemberVo> pageInfo = new PageInfo<>(memberList);

        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("pageNum", pageInfo.getPageNum());
        map.put("members", pageInfo.getList());
        return map;
    }

    /**
     * 根据主键查询会员表
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Member findById(Serializable id) {
        return memberMapper.selectById(id);
    }

    /**
     * 会员表新增
     * @param member
     */
    @Override
    public void add(Member member) {
        memberMapper.insert(member);
    }

    /**
     * 修改会员表
     * @param member
     */
    @Override
    public void modify(Member member) {
        memberMapper.update(member);
    }

    /**
     * 根据主键删除会员表
     * @param id
     */
    @Override
    public void removeById(Serializable id) {
        if(((String)id).contains(",")){
            memberMapper.deleteByIds(((String)id).split(","));
        }else {
            memberMapper.deleteById(id);
        }
    }

    @Override
    public Workbook export(int pageNum, int pageSize, Member member) {
        PageHelper.startPage(pageNum,pageSize);
        member.setIsDeleted(CommonConstant.IS_DELETED_NO);
        List<MemberVo> list = memberMapper.select(member);

        ExportParams params = new ExportParams("会员信息表", "会员表");
        Workbook workbook = ExcelExportUtil.exportExcel(params, MemberVo.class, list);
        return workbook;
    }

    @SneakyThrows
    @Override
    public void importExcel(MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);

        List<MemberVo> list = ExcelImportUtil.importExcel(file.getInputStream(), MemberVo.class, params);

        List<Member> memberList = list.stream().map(memberVo -> {
            Member member = new Member();
            BeanUtils.copyProperties(memberVo, member);
            member.setPassword(SecureUtil.md5(UserConstant.DEFAULT_PASSWORD));
            member.setCreateTime(LocalDateTime.now());
            member.setUpdateTime(LocalDateTime.now());
            member.setIsDeleted(CommonConstant.IS_DELETED_NO);
            return member;
        }).collect(Collectors.toList());

        // 批量插入
        memberMapper.insertBatch(memberList);
    }
}
